package com.game.sprite.components;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;

import com.game.sprite.BasicSprite;
import com.game.utils.ImageTool;

/**
* @author oven:
* @version 创建时间：2012-11-12 上午10:22:08
* 
*/
public class BasicNumber extends BasicSprite {
	public static byte SYMBOL_NONE=-1;
	public static byte SYMBOL_COLON=10;//冒号
	public static byte SYMBOL_PLUS=11;//加
	public static byte SYMBOL_MINUS=12;//减
	public static byte SYMBOL_MULTIPLY=13;//乘
	public static byte SYMBOL_EXCEPT=14;//除
	Bitmap[] myNums;
	int value;//值
	int realValue;
	byte symbol = SYMBOL_NONE;//默认无符号
	int space=0;//数字间隔
	float eachWidth=0;
	float eachHeight=0;
	boolean forceSymbol=false;//强制显示符号
	
	
	public BasicNumber(int value,float x,float y,int space){
		if(value<0){
			//负数
			symbol=SYMBOL_MINUS;
		}
		setPostX(x);
		setPostY(y);
		basicRegion.setPostx(x);
		basicRegion.setPosty(y);
		this.value = Math.abs(value);
		this.realValue=value;
		this.space=space;
		initResource();
		drawRegion=false;
		drawCollectionRegion=false;
	}
	
	public BasicNumber(int value,float x,float y){
		this(value,x,y,0);
	}
	
	
	/**
	 * 重设值
	 * @param value
	 */
	public void resetValue(int value){
		if(value<0){
			//负数
			symbol=SYMBOL_MINUS;
		}
		this.value=Math.abs(value);
		this.realValue=value;
		int valueLength=(""+value).length();
		if(symbol!=SYMBOL_NONE){
			valueLength=valueLength+1;
		}
		setWidth(eachWidth*valueLength+(valueLength-1)*space);
		setHeight(eachHeight);
	}
	
	
	public void initResource() {
		Matrix mx = new Matrix();
		mx.postScale(0.5f, 0.3f);
		myNums=ImageTool.CutImage("mynumber.png", 1, 15,mx);
		eachWidth = myNums[0].getWidth();
		eachHeight = myNums[0].getHeight();
		int valueLength=(""+value).length();
		if(symbol!=SYMBOL_NONE){
			valueLength=valueLength+1;
		}
		setWidth(eachWidth*valueLength+(valueLength-1)*space);
		setHeight(eachHeight);
	}

	@Override
	protected void paint(Canvas canvas) {
		float x=getPostX();
		float y=getPostY();
		String strValue = ""+value;
		//绘制符号
		if(symbol!=SYMBOL_NONE){
			canvas.drawBitmap(myNums[symbol], x, y, paint);
			x=x+eachWidth+space;
		}
		for(int i=0;i<strValue.length();i++){
			char c = strValue.charAt(i);
			if( c >= '0' && c <= '9' ){
				canvas.drawBitmap(myNums[(c-'0')], x, y, paint);
				x=x+eachWidth+space;
			}
		}
	}

	public int getValue() {
		return value;
	}

	public void setValue(int value) {
		this.value = value;
	}

	public int getRealValue() {
		return realValue;
	}

	public void setRealValue(int realValue) {
		this.realValue = realValue;
	}
}

